﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;

namespace YouHP.Pages.Member.Group
{
    public class Create : YouHP.Pages.User.UserBasePage
    {
        protected GroupInfo GroupData { get; set; }

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected void HandleCommon()
        {
            this.AddLocation("用户管理中心", "/User/Index.aspx");
            this.AddLocation("创建圈子", "/User/Group/Create.aspx");
        }

        protected void HandlePost()
        {
            GroupInfo info = ValidateForm();
            if (info == null) return;
            bool succ = GroupBLL.Instance.AddGroup(info);
            if (succ)
            {
                this.AddMsgLine("创建圈子成功");
                this.SetUrl("GroupIndex.aspx");
                SetMetaRefresh();
                return;
            }

            this.AddErrLine("添加失败");
        }

        protected void HandleGet()
        {

        }

        GroupInfo ValidateForm()
        {
            GroupInfo info = new GroupInfo();
            info.AppUserId = UserData.Id;
            info.GroupName = RequestTools.GetFormString("groupname");
            info.CreatTime = DateTime.Now;
            if (info.GroupName.Length < 4)
            {
                this.AddErrLine("圈子名称不能小于四个字");
                return null;
            }
            info.Keyword = RequestTools.GetFormString("groupkeyword");
            info.Introduction = RequestTools.GetFormString("groupintro");
            info.BrowserPermission = RequestTools.GetFormInt("groupbrowser", Constants.BrowserOpen);
            if (info.BrowserPermission != Constants.BrowserOpen || info.BrowserPermission != Constants.BrowserLimit)
                info.BrowserPermission = Constants.BrowserOpen;
            info.JoinPermission = RequestTools.GetFormInt("groupjoin", Constants.JoinOpen);
            if (info.JoinPermission != Constants.JoinOpen || info.JoinPermission != Constants.JoinLimit)
                info.JoinPermission = Constants.JoinOpen;
            return info;
        }
    }
}